BUILD:=./build
HG_IMG_NAME:=hd.img

CFLAGS:= -m32
CFLAGS+= -masm=intel
CFLAGS+= -fno-builtin
CFLAGS+= -nostdinc
CFLAGS+= -fno-pic
CFLAGS+= -fno-pie
CFLAGS+= -nostdlib
CFLAGS+= -fno-stack-protector
CFLAGS+= -I./oskernel/include
CFLAGS:=$(strip $(CFLAGS))

CFLAGS64:= -m64
CFLAGS64+= -mcmodel=large
CFLAGS64+= -fno-builtin
CFLAGS64+= -nostdinc
CFLAGS64+= -fno-pic
CFLAGS64+= -fno-pie
CFLAGS64+= -nostdlib
CFLAGS64+= -fno-stack-protector
CFLAGS64+= -I./x64kernel/include
CFLAGS64:=$(strip $(CFLAGS64))

DEBUG:= -g

# boot.bin：0~0
# setup.bin: 1~2
# system.bin: 3~42
# ap_init.bin: 43~46预留在此
# system64.bin: 47~...
$(BUILD)/$(HG_IMG_NAME): $(BUILD)/boot/boot.bin $(BUILD)/boot/setup.bin \
	$(BUILD)/system.bin $(BUILD)/kernel64/system64.bin $(BUILD)/boot/ap_init.bin
	$(shell rm -fr $@)
	bximage -q -hd=16 -func=create -sectsize=512 -imgmode=flat $@
	dd if=$< of=$@ bs=512 seek=0 count=1 conv=notrunc
	dd if=$(BUILD)/boot/setup.bin of=$@ bs=512 seek=1 count=2 conv=notrunc
	dd if=$(BUILD)/system.bin of=$@ bs=512 seek=3 count=40 conv=notrunc
	dd if=$(BUILD)/boot/ap_init.bin of=$@ bs=512 seek=43 count=4 conv=notrunc
	dd if=$(BUILD)/kernel64/system64.bin of=$@ bs=512 seek=47 count=5000 conv=notrunc

################### 32位内核编译配置起始 ####################
$(BUILD)/system.bin: $(BUILD)/kernel.bin
	objcopy -O binary $(BUILD)/kernel.bin $(BUILD)/system.bin
	nm $(BUILD)/kernel.bin | sort > $(BUILD)/system.map

$(BUILD)/kernel.bin: $(BUILD)/boot/head.o $(BUILD)/init/main.o \
	$(BUILD)/kernel/asm/io.o $(BUILD)/kernel/chr_drv/console.o \
	$(BUILD)/kernel/printk.o $(BUILD)/kernel/vsprintf.o \
	$(BUILD)/lib/string.o $(BUILD)/kernel/asm/enter_x64.o \
	$(BUILD)/kernel/gdt.o
	ld -m elf_i386 $^ -o $@ -Ttext 0x1200

$(BUILD)/kernel/%.o: oskernel/kernel/%.c
	$(shell mkdir -p $(BUILD)/kernel)
	gcc $(CFLAGS) $(DEBUG) -c $< -o $@

$(BUILD)/kernel/asm/%.o: oskernel/kernel/asm/%.asm
	$(shell mkdir -p $(BUILD)/kernel/asm)
	nasm -f elf32 -g $< -o $@

$(BUILD)/kernel/chr_drv/%.o: oskernel/kernel/chr_drv/%.c
	$(shell mkdir -p $(BUILD)/kernel/chr_drv)
	gcc $(CFLAGS) $(DEBUG) -c $< -o $@

$(BUILD)/lib/%.o: oskernel/lib/%.c
	$(shell mkdir -p $(BUILD)/lib)
	gcc $(CFLAGS) $(DEBUG) -c $< -o $@

$(BUILD)/init/main.o: oskernel/init/main.c
	$(shell mkdir -p $(BUILD)/init)
	gcc $(CFLAGS) $(DEBUG) -c $< -o $@

$(BUILD)/boot/head.o: oskernel/boot/head.asm
	nasm -f elf32 -g $< -o $@

$(BUILD)/boot/%.bin: oskernel/boot/%.asm
	$(shell mkdir -p $(BUILD)/boot)
	nasm $< -o $@
################### 32位内核编译配置截止 ####################

################### 64位内核编译配置起始 ####################
$(BUILD)/kernel64/system64.bin: $(BUILD)/kernel64/kernel64.bin
	objcopy -O binary $< $@
	nm $< | sort > $(BUILD)/kernel64/system.map

$(BUILD)/kernel64/kernel64.bin: $(BUILD)/kernel64/boot/head.o $(BUILD)/kernel64/init/main.o \
	$(BUILD)/kernel64/kernel/chr_drv/console.o \
	$(BUILD)/kernel64/kernel/printk.o $(BUILD)/kernel64/kernel/vsprintf.o \
	$(BUILD)/kernel64/lib/string.o $(BUILD)/kernel64/boot/printk.o \
	$(BUILD)/kernel64/mm/memory.o $(BUILD)/kernel64/kernel/bitmap.o \
	$(BUILD)/kernel64/kernel/assert.o $(BUILD)/kernel64/mm/malloc.o \
	$(BUILD)/kernel64/kernel/idt.o $(BUILD)/kernel64/kernel/asm/interrupt_handler.o \
	$(BUILD)/kernel64/kernel/8259a.o $(BUILD)/kernel64/kernel/chr_drv/keyboard.o \
	$(BUILD)/kernel64/kernel/exception.o $(BUILD)/kernel64/kernel/time.o \
	$(BUILD)/kernel64/kernel/task.o $(BUILD)/kernel64/kernel/asm/sched.o \
	$(BUILD)/kernel64/kernel/interrupt/clock_interrupt.o $(BUILD)/kernel64/kernel/sched.o \
	$(BUILD)/kernel64/kernel/asm/clock_handler.o $(BUILD)/kernel64/kernel/acpi.o \
	$(BUILD)/kernel64/mm/page.o $(BUILD)/kernel64/kernel/apic.o $(BUILD)/kernel64/kernel/gdt.o \
	$(BUILD)/kernel64/kernel/cpu.o $(BUILD)/kernel64/kernel/asm/cpu_broadcast_handler_entry.o \
	$(BUILD)/kernel64/kernel/asm/time_slice_handler_entry.o \
	$(BUILD)/kernel64/kernel/asm/sched64.o
	$(shell mkdir -p $(BUILD)/kernel64) 
	ld -b elf64-x86-64 -o $@ $^ -Ttext 0x100000

$(BUILD)/kernel64/boot/%.o: x64kernel/boot/%.asm
	$(shell mkdir -p $(BUILD)/kernel64/boot)
	nasm -f elf64 -g $< -o $@

$(BUILD)/kernel64/kernel/%.o: x64kernel/kernel/%.c
	$(shell mkdir -p $(BUILD)/kernel64/kernel)
	gcc $(CFLAGS64) $(DEBUG) -c $< -o $@

$(BUILD)/kernel64/kernel/asm/%.o: x64kernel/kernel/asm/%.asm
	$(shell mkdir -p $(BUILD)/kernel64/kernel/asm)
	nasm -f elf64 -g $< -o $@

$(BUILD)/kernel64/kernel/chr_drv/%.o: x64kernel/kernel/chr_drv/%.c
	$(shell mkdir -p $(BUILD)/kernel64/kernel/chr_drv)
	gcc $(CFLAGS64) $(DEBUG) -c $< -o $@

$(BUILD)/kernel64/kernel/interrupt/%.o: x64kernel/kernel/interrupt/%.c
	$(shell mkdir -p $(BUILD)/kernel64/kernel/interrupt)
	gcc $(CFLAGS64) $(DEBUG) -c $< -o $@

$(BUILD)/kernel64/lib/%.o: x64kernel/lib/%.c
	$(shell mkdir -p $(BUILD)/kernel64/lib)
	gcc $(CFLAGS64) $(DEBUG) -c $< -o $@

$(BUILD)/kernel64/init/%.o: x64kernel/init/%.c
	$(shell mkdir -p $(BUILD)/kernel64/init)
	gcc $(CFLAGS64) $(DEBUG) -c $< -o $@

$(BUILD)/kernel64/mm/%.o: x64kernel/mm/%.c
	$(shell mkdir -p $(BUILD)/kernel64/mm)
	gcc $(CFLAGS64) $(DEBUG) -c $< -o $@
################### 64位内核编译配置截止 ####################

all: $(BUILD)/$(HG_IMG_NAME)

clean:
	$(shell rm -rf $(BUILD))

qemu: $(BUILD)/$(HG_IMG_NAME)
	qemu-system-x86_64 \
	-m 1G \
	-boot d \
	-cpu Haswell -smp cores=1,threads=3 \
	-hda ./build/hd.img

qemug: $(BUILD)/$(HG_IMG_NAME)
	qemu-system-x86_64 \
	-m 1G \
	-boot d \
	-cpu Haswell -smp cores=1,threads=2 \
	-hda ./build/hd.img -s -S

bochs: $(BUILD)/$(HG_IMG_NAME)
	bochs -q -f bochs.bxrc